﻿using System;
using System.Linq;
using ApplLogic.ApplClasses;
using ApplLogic.ApplDbMediators;
using System.Collections.Generic;
using Utilities.GlobalData;
using Utilities.XmlReader;

namespace ApplLogic.LogicalServices
{
    public class ApplManagerRequestHandler : ApplRequestHandlerAbstract
    {
        private ApplAlgoExecuter m_ApplAlgoExecuter = null;
        private EmailHandler m_EmailHandler = null;

        public ApplManagerRequestHandler()
        {
            m_ApplAlgoExecuter = new ApplAlgoExecuter();
            m_EmailHandler = new EmailHandler();
        }

        #region FirstConfigurationMethods

        public void ChangeDefaultPeriodBetweenShiftsForRunningAlgo(string newPeriod)
        {
            MyXmlReader.ChangeSingleConstraint(
                eConstraints.PERIODBETWEENSHIFTS, newPeriod);
        }

        public void ChangeNumberOfShiftsPerDay(string numberOfShiftsPerDay)
        {
            MyXmlReader.ChangeSingleValueOfGeneralSetting(
                eGeneralSettingName.NumberOfShiftsPerDay, numberOfShiftsPerDay);
        }

        public void ChangeLenghtOfShift(string lenghtOfShift)
        {
             MyXmlReader.ChangeSingleValueOfGeneralSetting(
                eGeneralSettingName.LenghtOfShiftInHours, lenghtOfShift);
        }

        public void ChangeStartTimeOfWork(string startTimeOfWork)
        {
            MyXmlReader.ChangeSingleValueOfGeneralSetting(
               eGeneralSettingName.StartTimeOfWork, startTimeOfWork);
        }

        public void ChangeNumberOfWorkersPerShift(string numberOfWorkersPerShift)
        {
             MyXmlReader.ChangeSingleValueOfGeneralSetting(
                eGeneralSettingName.NumberOfWorkersPerShift, numberOfWorkersPerShift);
        }

        #endregion


        public void AddWorker(ApplWorker workerToAdd)
        {
            ApplDbWorkerMediator.Instance.AddWorker(workerToAdd, true);
        }

        public void DeleteWorker(string workerId)
        {
            ApplDbWorkerMediator.Instance.DeleteWorker(workerId, true);
        }

        public void AddShift(ApplShift shiftToAdd)
        {
            shiftToAdd.Id = ApplDbShiftMediator.Instance.GenerateIdForShift();
            ApplDbShiftMediator.Instance.AddShift(shiftToAdd, true);

        }

        public void DeleteShift(string shiftId)
        {
            ApplDbShiftMediator.Instance.DeleteShift(shiftId, true);
        }

        public void UpdateShift(ApplShift updatedShift)
        {
            ApplDbShiftMediator.Instance.UpdateShift(updatedShift);
        }

        public void AddWorkerStat(ApplWorkerStatInWeek applWorkerStat)
        {
            ApplDbWorkerStatMediator.Instance.AddWorkerStat(applWorkerStat);
        }

        public void DeleteStatInDateRangeForAllWorkers(DateTime startTime, DateTime endTime)
        {
            ApplDbWorkerStatMediator.Instance.DeleteStatInDateRangeForAllWorkers(startTime, endTime);
        }

        public void AddResultShiftToWorker(string workerId, DateTime startTime, DateTime endTime)
        {
            ApplDbResultMediator.Instance.AddResultShiftToWorker(workerId, startTime, endTime, eResult.BYMANAGER, true);
        }

        public void DeleteResult(string workerId, DateTime startTime, DateTime endTime)
        {
            ApplDbResultMediator.Instance.DeleteResult(workerId, startTime, endTime, true);
        }

        public void ChangeResultRegistrationOfAworker(string workerId, DateTime startTimeToremove, DateTime endTimeToRemove,
            DateTime startTimeToAdd, DateTime endTimeToAdd)
        {
            ApplDbResultMediator.Instance.AddResultShiftToWorker(workerId, startTimeToAdd, endTimeToAdd, eResult.BYMANAGER, true);
            ApplDbResultMediator.Instance.DeleteResult(workerId, startTimeToremove, endTimeToRemove, true);
        }
 
        public void AddSingleShiftResult(ApplSingleResult singleResultToAdd)
        {
            ApplDbResultMediator.Instance.AddResultShiftToWorkerWithStep(singleResultToAdd, true);
        }

        public void UpdateSingleShiftResult(ApplSingleResult singleResultToUpdate)
        {
            ApplDbResultMediator.Instance.UpdateResultType(singleResultToUpdate, true);
        }

        public void DeleteSingleShiftResult(string workerId, string shiftId)
        {
            ApplDbResultMediator.Instance.DeleteResult(workerId, shiftId, true);
        }

        public void RunAlgo(DateTime startTime, DateTime endTime)
        {
           m_ApplAlgoExecuter.RunAlgo(startTime, endTime);
        }

        public void NotifyAllWorkersForNewShiftCreation(DateTime startTime, DateTime endTime, string additionalInfo)
        {
            m_EmailHandler.NotifyAllWorkersForNewShiftCreation(startTime, endTime, additionalInfo);
        }

        public void NotifyWorkersThatnNotSendRequests(DateTime startTime, DateTime endTime, string additionalInfo)
        {
            m_EmailHandler.NotifyWorkersThatnNotSendRequests(startTime, endTime, additionalInfo);
        }

        public List<ApplWorker> GetAllWorkersThatAskedForShiftsInRange(DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllWorkersThatAskedForShiftsInRange(startTime, endTime);
        }

        public List<ApplWorker> GetAllWorkersThatNotAskedForanyShiftInRange(DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllWorkersThatNotAskedForanyShiftInRange(startTime, endTime);
        }

        public Dictionary<ApplWorker, List<ApplSingleRequest>> GetAllRequestsOfAllWorkers(DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllRequestsOfAllWorkers(startTime, endTime);
        }

        public Dictionary<ApplWorker, List<ApplShift>> GetAllRequestsOfAllWorkersWithType(eType type, DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllRequestsOfAllWorkersWithType(type, startTime, endTime);
        }

        public Dictionary<ApplShift, List<ApplSingleRequest>> GetAllRequestsOfAllShifts(DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllRequestsOfAllShifts(startTime, endTime);
        }

        public Dictionary<ApplShift, List<ApplWorker>> GetAllRequestsOfAllShiftsWithType(eType type, DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllRequestsOfAllShiftsWithType(type, startTime, endTime);
        }

        public Dictionary<ApplWorker, List<ApplSingleResult>> GetAllResultsOfAllWorkers(DateTime startTime, DateTime endTime)
        {
            return ApplDbResultMediator.Instance.GetAllResultsOfAllWorkers(startTime, endTime);
        }

        public Dictionary<ApplWorker, List<ApplShift>> GetAllResultsOfAllWorkersWithResult(eResult result, DateTime startTime, DateTime endTime)
        {
            return ApplDbResultMediator.Instance.GetAllResultsOfAllWorkersWithResult(result, startTime, endTime);
        }

        public Dictionary<ApplShift, List<ApplSingleResult>> GetAllResultsOfAllShifts(DateTime startTime, DateTime endTime)
        {
            return ApplDbResultMediator.Instance.GetAllResultsOfAllShifts(startTime, endTime);
        }

        public Dictionary<ApplShift, List<ApplWorker>> GetAllResultsOfAllShiftsWithResult(eResult result, DateTime startTime, DateTime endTime)
        {
            return ApplDbResultMediator.Instance.GetAllResultsOfAllShiftsWithResult(result, startTime, endTime);
        }

        public List<ApplWorker> GetAllWorkersThatGotShift(string shiftId, DateTime startTime, DateTime endTime)
        {
            return ApplDbResultMediator.Instance.GetAllWorkersThatGotShift(shiftId, startTime, endTime);
        }

        public List<ApplWorker> GetAllWorkersThatGotShiftInStep(string shiftId, eResult result, DateTime startTime, DateTime endTime)
        {
            return ApplDbResultMediator.Instance.GetAllWorkersThatGotShiftWithStep(shiftId, result, startTime, endTime);
        }

        public List<ApplWorker> GetAllWorkersThatAskedForAShiftWithType(string shiftId, eType type, DateTime startTime, DateTime endTime)
        {
            return ApplDbRequestMediator.Instance.GetAllWorkersThatAskedForShiftWithType(shiftId, type, startTime, endTime);
        }

        public List<ApplWorker> GetAllWorkers()
        {
            return ApplDbWorkerMediator.Instance.GetAllWorkers();
        }
    }
}
